<?php

/*
* PHPSDL - Testing sample for Image loading and scrolling
*
* Achja, there isn't much checking done. Make sure the image has at least
* a height of 200 pixel and a minimum width of 320 pixel. Actually, if you
* want the thing to scroll, make sure the image is wider ;-)
*
* Markus Fischer <mfischer@guru.josefine.at>
*
*/

set_time_limit(0);
dl("php_sdl.dll");

error_reporting(E_ALL);

SDL_Init(SDL_INIT_VIDEO);

if (false === ($scroll_image = SDL_LoadBMP('background.bmp')))
	exit;
	
if (false === ($screen = SDL_SetVideoMode(320, 200, 0, 0)))
	exit;

if ($new_image = SDL_DisplayFormat($scroll_image)) {
	$scroll_image = $new_image;
}
	
$mouse_x = $screen['w'] >> 1;
$mouse_y = $screen['h'] >> 1;

SDL_WarpMouse($mouse_x, $mouse_y);
SDL_ShowCursor(true);
SDL_WM_GrabInput(SDL_GRAB_ON);

$mainloop = true;
$scroll_image_x_offset = 0;

update_screen();

while ($mainloop) {
	while (SDL_PollEvent(&$event)) {
		switch ($event['type']) {
			case SDL_MOUSEMOTION:
				if ($event['motion']['xrel'] == 0)
					break;
				$scroll_image_x_offset += $event['motion']['xrel'];
				update_screen();
				break;
			case SDL_KEYDOWN:
				if ($event['key']['keysym']['sym'] != SDLK_ESCAPE)
					break;
				/* Break missing intentionally */
			case SDL_QUIT:
				$mainloop = false;
				break;
		}
	}
}

function update_screen() {
	global $screen, $scroll_image_x_offset, $scroll_image;

	if ($scroll_image_x_offset > $scroll_image['w'] - $screen['w']) {
		$scroll_image_x_offset = $scroll_image['w'] - $screen['w'];
		return;
	}

	if ($scroll_image_x_offset < 0) {
		$scroll_image_x_offset = 0;
		return;
	}

	$r['x'] = $scroll_image_x_offset;
	$r['y'] = 0;
	$r['w'] = $screen['w'];
	$r['h'] = $screen['h'];
	SDL_BlitSurface($scroll_image, $r, $screen, NULL);

	SDL_UpdateRect($screen, 0, 0, 0, 0);
}

?>
